﻿using System;
using System.Collections.Generic;
//using System.Data;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web;
using System.Web.Http;
using LynxBlogAPI.Models;

namespace LynxBlogAPI.Controllers
{
    public class BlogLayoutDataController : ApiController
    {
        private LynxWebLog_Context db = new LynxWebLog_Context();

        // GET api/BlogLayoutData
        [Queryable]
        public IEnumerable<BlogLayoutData> GetBlogLayoutData()
        {
            return db.BlogLayoutDatas.AsEnumerable();
        }

        // GET api/BlogLayoutData/5
        [Queryable]
        public BlogLayoutData GetBlogLayoutData(int id)
        {
            BlogLayoutData BlogLayoutData = db.BlogLayoutDatas.Find(id);
            if (BlogLayoutData == null) {
                throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound));
            }

            return BlogLayoutData;
        }

        // PUT api/BlogLayoutData/5
        public HttpResponseMessage PutBlogLayoutData(int id, BlogLayoutData BlogLayoutData)
        {
            if (!ModelState.IsValid) {
                return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
            }

            if (id != BlogLayoutData.bloglayoutid) {
                return Request.CreateResponse(HttpStatusCode.BadRequest);
            }

            db.Entry(BlogLayoutData).State = EntityState.Modified;

            try {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException ex) {
                return Request.CreateErrorResponse(HttpStatusCode.NotFound, ex);
            }

            return Request.CreateResponse(HttpStatusCode.OK);
        }

        // POST api/BlogLayoutData
        public HttpResponseMessage PostBlogLayoutData(BlogLayoutData BlogLayoutData)
        {
            if (ModelState.IsValid) {
                db.BlogLayoutDatas.Add(BlogLayoutData);
                try {
                    db.SaveChanges();
                }
                catch (SystemException ex) {
                    Request.CreateErrorResponse(HttpStatusCode.InternalServerError, ex);
                }

                HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, BlogLayoutData);
                response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = BlogLayoutData.bloglayoutid }));
                return response;
            } else {
                return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
            }
        }

        // DELETE api/BlogLayoutData/5
        public HttpResponseMessage DeleteBlogLayoutData(int id)
        {
            BlogLayoutData BlogLayoutData = db.BlogLayoutDatas.Find(id);
            if (BlogLayoutData == null) {
                return Request.CreateResponse(HttpStatusCode.NotFound);
            }

            db.BlogLayoutDatas.Remove(BlogLayoutData);

            try {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException ex) {
                return Request.CreateErrorResponse(HttpStatusCode.NotFound, ex);
            }

            return Request.CreateResponse(HttpStatusCode.OK, BlogLayoutData);
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}
